package com.cjb.cloudsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjb.cloudhdfs.api.HadoopUtil;
import com.cjb.cloudsystem.entity.SysObject;
import com.cjb.cloudsystem.enums.FileBasicPathEnum;
import com.cjb.cloudsystem.mapper.SysObjectMapper;
import com.cjb.cloudsystem.service.SysObjectService;
import com.cjb.cloudsystem.vo.FileVO;
import com.cjb.cloudsystem.vo.SysObjectVO;
import com.cjb.common.exception.ServerException;
import com.cjb.common.mybatisUtil.impl.BaseRepositoryImpl;
import com.cjb.common.util.Common;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author chenjinbin
 * @Package IntelliJ IDEA
 * @Class SysObjectsServiceImpl
 * @Date 2023-07-27 21:41
 */
@Service
@AllArgsConstructor
public class SysObjectRepositoryImpl extends BaseRepositoryImpl<SysObjectMapper, SysObject> implements SysObjectService {
    /**
     * 日志
     */
    private final static Logger log = LoggerFactory.getLogger(SysObjectRepositoryImpl.class);


    /**
     * 注入HadoopAPI
     */
    @Resource
    private  HadoopUtil hadoopUtil;


    /**
     * 单体保存
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public boolean save(SysObject sysObject)  {
        if (Common.isEmpty(sysObject.getSysObjectPath()) || Common.isEmpty(sysObject.getSysObjectName())||
        Common.isEmpty(sysObject)){
            throw new ServerException("sysObject is empty");
        }
        /**
         * 插入数据库
         */
        baseMapper.insert(sysObject);
        return true;
    }


    /**
     * 批量保存
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public Integer listSave(List<SysObject> sysObject) {
        return baseMapper.insertBatchSomeColumn(sysObject);
    }

    /**
     * 批量更新
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public void listUpdate(List<SysObject> sysObject) {
        // 执行批量更新操作
        saveOrUpdateBatch(sysObject,sysObject.size());
    }

    /**
     * 批量删除
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public void listDelete(List<SysObject> sysObject) {

        if (Common.isNotEmpty(sysObject)){
            sysObject.forEach(o ->{
                if (Common.isNotEmpty(o.getSysObjectPath())){
                    try {
                        hadoopUtil.delDir(o.getSysObjectPath());
                    } catch (Exception e) {
                        new ServerException("删除目录异常");
                    }
                }
            });
            List<Long> collect = sysObject.stream().map(SysObject::getSysObjectId).distinct().collect(Collectors.toList());
            baseMapper.deleteBatchIds(collect);
        }
        else {
            new ServerException("参数为空");
        }
    }

    /**
     * 通过id删除
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public int deleteById(SysObject sysObject) {
        return baseMapper.deleteById(sysObject.getSysObjectId());
    }

    /**
     * 单个查询
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public SysObject queryById(SysObject sysObject) {
        return baseMapper.selectById(sysObject.getSysObjectId());
    }

    /**
     * 分页
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public Page queryPage(Page sysObjectPage, SysObjectVO sysObjectVO) {
        return baseMapper.selectPage(sysObjectPage, getWrapper(sysObjectVO));
    }

    /**
     * 查询属性
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    private LambdaQueryWrapper<SysObject> getWrapper(SysObjectVO query) {
        LambdaQueryWrapper<SysObject> wrapper = Wrappers.lambdaQuery();
        if(Common.isNotNull(query.getSysObjectId())){wrapper.eq(SysObject::getSysObjectId,query.getSysObjectId());}
        if(Common.isNotNull(query.getSysObjectPath())){wrapper.like(SysObject::getSysObjectPath,"%"+query.getSysObjectPath()+"%");}
        if(Common.isNotNull(query.getSysObjectName())){wrapper.like(SysObject::getSysObjectName,"%"+query.getSysObjectName()+"%");}
        if(Common.isNotNull(query.getSysOrgId())){wrapper.eq(SysObject::getSysOrgId,query.getSysOrgId());}
        if(Common.isNotNull(query.getIsEncrytion())){wrapper.eq(SysObject::getIsEncrytion,query.getIsEncrytion());}
        if(Common.isNotNull(query.getDeleted())){wrapper.eq(SysObject::getDeleted,query.getDeleted());}
        if(Common.isNotNull(query.getCreator())){wrapper.eq(SysObject::getCreator,query.getCreator());}
        if(Common.isNotNull(query.getUpdater())){wrapper.eq(SysObject::getUpdater,query.getUpdater());}
        if(Common.isNotNull(query.getCreateTime())){wrapper.eq(SysObject::getCreateTime,query.getCreateTime());}
        if(Common.isNotNull(query.getUpdateTime())){wrapper.eq(SysObject::getUpdateTime,query.getUpdateTime());}
        if(Common.isNotNull(query.getProductionId())){wrapper.eq(SysObject::getProductionId,query.getProductionId());}
        wrapper.orderByDesc(SysObject::getCreateTime);

        return wrapper;
    }


    /**
     * 返回全部信息
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public List<SysObject> returnAllData() {
        return baseMapper.selectList(null);
    }

    /**
     * readFile
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public void readFile(String filePath) {
        if (Common.isNotEmpty(filePath)){
            try {
                hadoopUtil.readFile(filePath);
            } catch (Exception e) {
                throw new ServerException("读取失败"+e);
            }
        }
    }

    /**
     * upload
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public FileVO upload(MultipartFile file, String dir) throws Exception {
        if (Common.isEmpty(file)){
            new ServerException("文件上传是空的");
        }
        if (Common.isEmpty(dir)){
            dir = file.getOriginalFilename();
        }
        hadoopUtil.uploadFile(file.getBytes(),dir);

        //传入结构体
        FileVO fileVO = new FileVO();
        fileVO.setFileContentType(file.getContentType());

        fileVO.setFilePath(FileBasicPathEnum.BASIC_PATH.getValue()+file.getOriginalFilename());
        fileVO.setFileSize(file.getSize());
        fileVO.setFileName(file.getOriginalFilename());
        return fileVO;
    }
    /**
     * downLoad
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public void downLoad(String filePath) {
        if (Common.isNotEmpty(filePath)){
            try {
                hadoopUtil.download(filePath);
            } catch (Exception e) {
                throw new ServerException("下载失败"+e);
            }
        }
       else {
           new ServerException("路径参数为空");
        }
    }

}